package com.mmall.controller.test;

import com.mmall.pojo.DeptInfo;
import com.mmall.pojo.ListInfode;
import com.mmall.pojo.User;
import com.mmall.util.DateTimeUtil;
import com.mmall.util.ExcelOutUtilByList;
import com.mmall.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@Slf4j
public class RequestUtils {
    /**
     * 用get方式连接url

     * @return 从url获得的数据
     */

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(RequestUtils.class);

    //public static String CallWebPageGet(String urlString,String pdata) {
    public static String CallWebPageGet() {
        String result="";
        PrintWriter out = null;
        BufferedReader in = null;
        URL url=null;
        try {
            url = new URL("http://nb.waterlabel.org.cn/api/rrecord/public/list?prodType=13&prodModel=&entName=&pageNum=1&pageSize=1000000");//用url路径以及所用参数创建URL实例类
            URLConnection connect = url.openConnection();//创建连接
            connect.setRequestProperty("content-type","application/x-www-form-urlencoded;charset=utf-8");//设置请求header的属性--请求内容类型
            connect.setRequestProperty("method","GET");//设置请求header的属性值--请求方式
            // 建立实际的连接
            connect.connect();  //建立与url所在服务器的连接
            // 获取所有响应头字段
            Map<String, List<String>> map = connect.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
            String line="";
            while ((line = in.readLine()) != null) {
                result +=  line;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //
//User user = JsonUtil.string2Obj(user1Json,User.class);
        //ListInfo data =   objectMapper.readValue(result,ListInfo.class);
        //List<DeptInfo> userListObj1 = JsonUtil.string2Obj(result, new TypeReference<List<DeptInfo>>() {
        //});


        //List<DeptInfo> userListObj2 = JsonUtil.string2Obj(result,List.class,DeptInfo.class);


//        ObjectMapper mapper = new ObjectMapper();
//        try {
//            ListInfo readValue = mapper.readValue(result,ListInfo.class);
//            List<DeptInfo> list = (List<DeptInfo>) readValue.getList();
//            String test = list.get(0).getEntName();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


        ListInfo readValue = JsonUtil.string2Obj(result,ListInfo.class);
        List<DeptInfo> list = (List<DeptInfo>) readValue.getList();
        String test = list.get(0).getEntName();

        for (int i =0;i<list.size();i++){
            List<ListInfode> listde = CallWebPageGet1(list.get(i).getProdRecordno());

            list.get(i).setProducerName(listde.get(0).getValue());
            list.get(i).setFlow(listde.get(1).getValue());
        }

//        try {
//            ExcelOutUtilByList.out("test","D:\\dcexcel",list,null,"");
//        }catch (Exception e){
//            logger.error("localCache get error",e);
//        }
        ExcelOutUtilByList.out("test","D:\\dcexcel",list,null,"");

        return result;
    }


    public static List<ListInfode> CallWebPageGet1(String pdata) {
        String result="";
        PrintWriter out = null;
        BufferedReader in = null;
        URL url=null;
        try {
            //http://nb.waterlabel.org.cn/api/rrecord/public/detail?prodRecordno=202303-13-131300-4884639147&ec_model_id=13
            url = new URL("http://nb.waterlabel.org.cn/api/rrecord/public/detail?prodRecordno="+pdata+"&ec_model_id=13");//用url路径以及所用参数创建URL实例类
            URLConnection connect = url.openConnection();//创建连接
            connect.setRequestProperty("content-type","application/x-www-form-urlencoded;charset=utf-8");//设置请求header的属性--请求内容类型
            connect.setRequestProperty("method","GET");//设置请求header的属性值--请求方式
            // 建立实际的连接
            connect.connect();  //建立与url所在服务器的连接
            // 获取所有响应头字段
            Map<String, List<String>> map = connect.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
            String line="";
            while ((line = in.readLine()) != null) {
                result +=  line;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        List<ListInfode> userListObj2 = JsonUtil.string2Obj(result,List.class,ListInfode.class);

        return userListObj2;
    }








    /**
     * 用gpost方式连接url
     * @param urlString url路径
     * @param pdata    url参数
     * @return 从url获得的数据
     */
    public static String CallWebPagePost(String urlString,String pdata) {
        String result="";
        PrintWriter out = null;
        BufferedReader in = null;
        URL url=null;
        try {
            url = new URL(urlString);
            URLConnection connect = url.openConnection();
            connect.setRequestProperty("content-type","application/x-www-form-urlencoded;charset=utf-8");
            connect.setRequestProperty("method","POST");
            byte[] bytes= pdata.getBytes("utf-8") ;
            connect.setDoOutput(true);
            connect.setDoInput(true);

            out = new PrintWriter(connect.getOutputStream());
            // 发送请求参数
            out.print(pdata);
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result +=  line;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }
}